import java.util.ArrayList;
import java.util.Random;

public class Merchant {
	private Bank b;
	private long signedCoin;
	private ArrayList<Integer> zVector;
	private int k = 10;

	public Merchant(Bank b) {
		this.b = b;
	}

	// Called by the customer
	public void buy(long coin, long coinSignature) {
		// 4. Alice gives the pair (x, h(x)1/3 ) to Bob. Bob can verify that
		// this is a valid
		// coin by verifying the signature.
		System.out
				.println("Some customer bought something from the merchant " +
						"and payed cash(e-cash)");
		boolean valid = false;
		System.out.println("Coin:		" + coin);
		System.out.println("CoinSignature:	" + coinSignature);
		System.out.println("CoingSignature % b.n"+coinSignature %b.n);
		System.out.println("Public key:	"+ b.publicKey);
		System.out.println("coinSignature^PK: "+ Customer.pow(coinSignature, b.publicKey));
		System.out.println("coinSignature^PK % b.n: "+ (Customer.pow(coinSignature, b.publicKey) % b.n));
		System.out.println(Customer.h(coin));
		System.out.println(Customer.h(coin) % b.n);
		System.out.println((Customer.h(coin)*coinSignature) % b.n );
		System.out.println((Customer.h(coin)*Customer.pow(coinSignature,  b.publicKey)));
		System.out.println((coin*Customer.pow(coinSignature,  b.publicKey))%b.n);
		System.out.println("Something random:"+((coinSignature % coin) == b.publicKey));
		System.out.println("pow(coin, PK)"+(Customer.pow(Customer.h(coin),  b.publicKey)));
		System.out.println("pow(coin, PK)"+(Customer.pow(Customer.h(coin),  b.publicKey))%b.n);
		if (Customer.h(coin) % b.n == (Customer.pow(coinSignature,  b.publicKey)) % b.n) {
			System.out.println("Coin: " + coin);
			System.out.println("CoinSignature: " + coinSignature);
			valid = true;
		}
		System.out.println("The validity of the coin was proven to be: "
				+ valid);
	}

	/**
	 * For cut and choose - returns a challenge vector
	 * @param signedCoin
	 * @return challenge vector
	 */
	public ArrayList<Integer> buy(long signedCoin) {
		zVector = new ArrayList<Integer>();
		Random rand = new Random();
		for (int i = 0; i < k; i++) {
			zVector.add(rand.nextInt());
		}
		this.signedCoin = signedCoin;
		return zVector;

	}
}
